package com.yearEndWorkload.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yearEndWorkload.common.Result;
import com.yearEndWorkload.mapper.SpecializedChildMapper;
import com.yearEndWorkload.pojo.entity.OutstandingPerformance;
import com.yearEndWorkload.pojo.entity.SpecializedChild;
import com.yearEndWorkload.pojo.entity.SpecializedWork;
import com.yearEndWorkload.pojo.excel.exports.NewOutstandingExportExcel;
import com.yearEndWorkload.pojo.excel.exports.SpecialWorkExportExcel;
import com.yearEndWorkload.pojo.form.add.AddSpecializedWorkChildForm;
import com.yearEndWorkload.pojo.form.add.AddSpecializedWorkForm;
import com.yearEndWorkload.pojo.form.update.UpdateSpecializedWorkChildForm;
import com.yearEndWorkload.pojo.form.update.UpdateSpecializedWorkForm;
import com.yearEndWorkload.pojo.query.SpecializedWorkQuery;
import com.yearEndWorkload.pojo.vo.*;
import com.yearEndWorkload.service.SpecializedWorkService;
import com.yearEndWorkload.mapper.SpecializedWorkMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author admin
* @description 针对表【specialized_work(专项工作绩效管理)】的数据库操作Service实现
* @createDate 2024-03-06 22:47:01
*/
@Service
public class SpecializedWorkServiceImpl extends ServiceImpl<SpecializedWorkMapper, SpecializedWork>
    implements SpecializedWorkService{
    
    @Autowired
    SpecializedWorkMapper specializedWorkMapper;

    @Autowired
    SpecializedChildMapper specializedChildMapper;

    @Override
    public Result getList(SpecializedWorkQuery specializedWorkQuery) {
        Page<SpecializedWorkVo> page = new Page<>(specializedWorkQuery.getCurrentPage(),specializedWorkQuery.getPageSize());
        page = specializedWorkMapper.getList(page,specializedWorkQuery);

        List<SpecializedWorkVo> recordsR = page.getRecords();

        BaseVo<BaseSpecializedWorkTreeVo> criteriaBaseVo = new BaseVo<>();

        criteriaBaseVo.setCurrentPage(specializedWorkQuery.getCurrentPage());
        criteriaBaseVo.setPageSize(specializedWorkQuery.getPageSize());
        criteriaBaseVo.setAllPages(page.getPages());
        criteriaBaseVo.setAllRecordNum(page.getTotal());

        List<BaseSpecializedWorkTreeVo> records= new ArrayList<>();

        for (int i = 0; i < recordsR.size(); i++) {
            List<SpecializedChildVo> childVoList= specializedChildMapper.selectListByProjectId(recordsR.get(i).getId(),specializedWorkQuery);

            BaseSpecializedWorkTreeVo specializedWorkTreeVo = new BaseSpecializedWorkTreeVo();
            specializedWorkTreeVo.setSpecializedWorkVo(recordsR.get(i));
            specializedWorkTreeVo.setChildVos(childVoList);
            records.add(specializedWorkTreeVo);
            if(childVoList.size()==0){
                recordsR.get(i).setFlagAllocation("未分配");
            }else {
                recordsR.get(i).setFlagAllocation("已分配");
            }
        }
        criteriaBaseVo.setRecords(records);

        return Result.success(criteriaBaseVo);
    }

    @Override
    public Result addSpecializedWork(AddSpecializedWorkForm addSpecializedWorkForm) {
        SpecializedWork specializedWork = new SpecializedWork();
        BeanUtil.copyProperties(addSpecializedWorkForm,specializedWork);
        specializedWorkMapper.insert(specializedWork);
        return Result.success("添加成功");
    }

    @Override
    public Result updateSpecializedWork(UpdateSpecializedWorkForm updateSpecializedWorkForm) {
        SpecializedWork specializedWork = new SpecializedWork();
        BeanUtil.copyProperties(updateSpecializedWorkForm,specializedWork);
        specializedWorkMapper.updateById(specializedWork);
        return Result.success("修改成功");
    }

    @Override
    public Result deleteSpecializedWorkByIds(List<String> list) {
        specializedWorkMapper.deleteBatchIds(list);
        return Result.success("删除成功");
    }

    @Override
    public Result addSpecializedWorkChild(AddSpecializedWorkChildForm addSpecializedWorkChildForm) {
        SpecializedChild specializedChild = new SpecializedChild();
        BeanUtil.copyProperties(addSpecializedWorkChildForm,specializedChild);
        specializedChildMapper.insert(specializedChild);
        return  Result.success("添加成功");
    }

    @Override
    public Result updateSpecializedWorkChild(UpdateSpecializedWorkChildForm updateSpecializedWorkChildForm) {
        SpecializedChild specializedChild = new SpecializedChild();
        BeanUtil.copyProperties(updateSpecializedWorkChildForm,specializedChild);
        specializedChildMapper.updateById(specializedChild);
        return  Result.success("修改成功");

    }

    @Override
    public Result deleteSpecializedWorkChildByIds(List<String> list) {
        specializedChildMapper.deleteBatchIds(list);
        return Result.success("删除成功");
    }

    @Override
    public List<SpecialWorkExportExcel> getSpecializedWorkExportExcels() {
        List<SpecialWorkExportExcel> re = new ArrayList<>();
        List<SpecialWorkExportExcel> list = specializedWorkMapper.getSpecializedWorkExportExcels();

        //构造
        for (SpecialWorkExportExcel specialWorkExportExcel : list) {
            re.add(specialWorkExportExcel);
            Integer projectId = specialWorkExportExcel.getId();
            List<SpecialWorkExportExcel> temp = specializedChildMapper.getSpecializedWorkExportExcelsByProjectId(projectId);
            if (temp.size() > 0) {
                re.addAll(temp);
            }
        }

        return re;

    }

}




